<?php

use yii\helpers\Url;
$web = Url::base();
$this->title = '扩展分类';
?>
<script type="text/javascript" src="<?= $web ?>/js/jquery.tooltips.js"></script>
<script type="application/javascript">

    $(function ($) {
        //设置机构是否可选择
        var currentManager = <?=json_encode(\app\models\Manager::getCurrentManager()) ?>;
        var current_institution_code = currentManager.institution_code;
        if (current_institution_code != null && current_institution_code != '' && current_institution_code != '0') {
            $("#institution").attr("disabled", "disabled");
        }



    });

    //筛选
    function filter() {
        var institution_code = $("#institution").val();
        if (institution_code == null || institution_code == '') {
            institution_code = '0';
          //  alert('请选择机构');
          //  return;
        }
        var href = window.location.href;
        href = setUrlParam(href, 'inst', institution_code);

        window.location.href = href;
    }

    function refresh() {
        var href = decodeURIComponent(window.location.href);
        window.location.href = href;
    }

    //数据保存
    function saveData() {
        $ex1 = 0;
        $("#upload_exec_target").load(function () {
            if ($ex1 == 0) {
                try {
                    var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');
                    if (result.status == 0) {
                        if (result.data != null && result.data != '') {
                           // alert('操作成功！');
                            refresh();
                        }
                    } else {
                        var re = result.message;
                        alert(re);
                    }
                } catch (err) {
                    var re = "系统异常，请重试";
                    alert(re);
                }

                $ex1 = 1;

            }
        });
        $("#modaledit_submit_form").submit();
    }

    function onAddData() {
        var institution_code = $("#institution").val();
       /* if (institution_code == '' || institution_code == null) {
            $("#hint_message").text('请选择机构！');
            $("#hintMessageModal").modal('show');
            return;
        }*/
        addData();
    }
    //打开新增修改页面
    function showCategoryEdit(data) {
        $("#goodscategoryedit").html('');
        $("#myModalShowLabelInst").html(data.institution_code);
        requestHtml('goods-extend-category-part-view', data, true, function (responseHtml) {
            $("#goodscategoryedit").html(responseHtml);
            $("#myModalGoodsCategoryEdit").modal('show');
        });
    }
    //新增
    function addData() {

        var institution_code = $("#institution").val();
        var data = {};
        data.institution_code = institution_code;
        $("#myModalLabel").html("新增");
        showCategoryEdit(data);
    }

    //修改/查看详情
    function editData(id, institution_code) {
        var data = {};
        data.id = id;
        data.institution_code = institution_code;
        $("#myModalLabel").html("编辑/查看");
        showCategoryEdit(data);

    }

    //分类产品设置
    function editGoods(id, institution_code) {
        var data = {};
        data.id = id;
        data.institution_code = institution_code;

        $("#hideChildEdit").html('');
        requestHtml('goods-extend-category-goods-part-view', data, true, function (responseHtml) {
            $("#hideChildEdit").html(responseHtml);
        }, function () {
            $("#hideChildEdit").html('无数据');
        });
    }


    //同步分类(同步上级机构分类)
    function synGoodsCate() {

        var institution_code = $("#institution").val();
//        alert(institution_code);
//        return;
        var data = {};
        data.institution_code = institution_code;
        loading.show();
        requestJson("syn-goods-extend-category-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                filter();
                $("#hint_message").text('操作成功！');
            } else {
                $("#hint_message").text('操作失败！' + response.message);
            }
            $("#hintMessageModal").modal('show');
        });

    }


    //删除操作
    function delData(id) {
       if(confirm("是否删除此条分类!")) {
           var institution_code = $("#institution").val();
           if ((id != null && id != '') && (institution_code != null && institution_code != '')) {
               var data = {};
               data.id = id;
               data.institution_code = institution_code;
               loading.show();
               requestJson("delete-goods-extend-category-handler", data, true, function (response) {
                   loading.hide();
                   if (response.status == 0) {
                       $("#hint_message").text('操作成功！');
                       filter();
                   } else {
                       $("#hint_message").text('操作失败！');
                   }
                   $("#hintMessageModal").modal('show');
               });
           } else {
               $("#hint_message").text('需删除的数据有误，请重试！');
               $("#hintMessageModal").modal('show');
           }
       }
    }

    //更新下级机构数据对话框
    function updateChilddata(){
        //取得选择的机构
        var institution_code = $("#institution").val();
        if(institution_code==null || institution_code==''){
            alert("请选择机构！");
            return;
        }
        updateChildInstData("<?=Url::to(['institution/institution-childlist-part-view'])?>",institution_code,3);
    }

    //确定更新下级机构数据
    function confUpdateChildData(){
        confUpdateChildInstData("<?=Url::to(['institution/sys-under-institution-data'])?>",3);
    }

    //设置模态窗 1.当用户点击模态框外部时不会关闭 2.当按下 escape 键时不会关闭 3.当初始化时不显示模态框。
    $(function () {
        $('#myModalGoodsCategoryEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#modalChildInstList').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>
<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="synGoodsCate()"><i class="fa fa-refresh"></i>
                    同步上级机构商品扩展分类
                </button>
                <button class="btn btn-primary" type="button" onclick="onAddData()"
                        data-toggle="modal"><i class="fa fa-plus"></i> 新增
                </button>
                <?php if (\app\models\Permission::hasPermission("institution", "sys-under-institution-data")) {
                    ?>
                    <button class="btn btn-primary" type="button" onclick="updateChilddata()"><i class="fa fa-cog"></i>
                        更新下级机构数据
                    </button>
                    <?php
                } ?>
            </div>
            <div class="pull-right">
                <div class="form-group">
                    <label class="sr-only" for="exampleInputPassword2">机构</label>
                    <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'institution', 'currentInstitutionCode' => $institution_code, 'defaultText' => '——选择机构——', 'tagSourceType'=>3]) ?>
                </div>
                <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="goodsList">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:20%">分类名称</th>
                    <th style="width:20%">终端类型</th>
                    <th style="width:20%">标识符</th>
                    <th style="width:15%">图片</th>
                    <th style="width:10%">类型</th>
                    <th style="width:10%">是否启用</th>
                    <th style="width:25%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach($list as $item){
                        ?>
                    <tr>
                        <td><?=$item['name'] ?></td>
                        <td><?=$item['endpoint'] ?></td>
                        <td><?=$item['identifier_code'] ?></td>
                        <td>
                            <a href="javascript:void(0)" class="tooltips" rel="<?= $item['image_url'] ?>"
                               target="_blank">
                                <img src="<?= $item['image_url'] ?>" style="width: 3rem; height: 3rem;">
                            </a>
                        </td>
                        <td>
                            <?php
                            switch ($item['type'])
                            {
                                case 1:
                                    echo "专题活动";
                                break;
                                case 2:
                                    echo "商品分类";
                                    break;
                                default:
                                    echo "未知";
                            }
                            ?>
                        </td>
                        <td>
                            <?php
                            switch ($item['is_enabled'])
                            {
                                case 0:
                                    echo "禁用";
                                break;
                                case 1:
                                    echo "启用";
                                    break;
                                default:
                                    echo "未知";
                            }
                            ?>
                        </td>
                        <td>
                            <button class="btn btn-link btn-xs" type="button"
                                    onclick="editData('<?= $item['id'] ?>','<?= $item['institution_code'] ?>')"
                                    data-toggle="modal" >
                                编辑/查看
                            </button>
                            <?php
                                if($item['type']=='2'){
                                    ?>
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="editGoods('<?= $item['id'] ?>','<?= $item['institution_code'] ?>')"
                                            href="#myModalHideChildEdit"
                                            data-toggle="modal" >
                                        分类产品编辑
                                    </button>
                                    <?php
                                }
                            ?>
                            <button class="btn btn-link btn-xs" type="button"
                                    data-toggle="modal"
                                    data-target="#deleteModal"
                                    onclick="delData('<?= $item['id'] ?>','<?= $item['institution_code'] ?>')">
                                删除
                            </button>
                        </td>
                    </tr>
                 <?php
                    }
                 ?>

                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Modal ad Edit-->
<div class="modal fade" id="myModalGoodsCategoryEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel"></h4>
            </div>
            <div id="goodscategoryedit" class="modal-body">
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Modal hide child edit-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="myModalHideChildEdit" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">分类产品设置</h4>
            </div>
            <div id="hideChildEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 下级机构选择列表-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="modalChildInstList" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <input type="hidden" id="updateChild_SuperInstCode" value="" readonly>
                <h4 class="modal-title">下级机构选择</h4>
            </div>
            <div id="childInstList" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confUpdateChildData()"><i class="fa fa-save"></i> 确定
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>

